package org.nuplay;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import javax.swing.JLabel;

import com.illposed.osc.OSCMessage;

public class NuPlayWiiMoteConnector {
	private DatagramSocket client_socket;
	private DatagramPacket packet;
	private Integer channel;
	private NuPlayWiiMoteDevice device;
	private JLabel label;
	//
	private int target_roll = 0;
	private int target_pitch = 0;
	//
	private int current_roll = 0;
	private int current_pitch = 0;
	//
	private float[] last_x = new float[4];
	private float[] last_y = new float[4];
	
	

	public NuPlayWiiMoteConnector(Integer in_channel, NuPlayWiiMoteDevice in_device, InetAddress address, JLabel in_label) {
		this.label = in_label;
		this.channel = in_channel;
		this.device = in_device;
		try {
			client_socket = new DatagramSocket();
			client_socket.connect(address, 3333);
			packet = new DatagramPacket(new byte[255], 255);
		} catch (Exception e) {
			e.printStackTrace();
		}
		target_roll  = Math.round(device.nGetRoll());
		target_pitch  = Math.round(device.nGetPitch());
		current_roll = target_roll;
		current_pitch = target_pitch;
	}

	public void close() {
		client_socket.close();
		client_socket = null;
	}
	

	public void adjust() {
		target_roll  = Math.round(device.nGetRoll());
		target_pitch  = Math.round(device.nGetPitch());
		
		
		if (target_roll != current_roll) {
			if (target_roll > current_roll) {
				current_roll++;
			} else {
				current_roll--;
			}
		}
		if (target_pitch != current_pitch) {
			if (target_pitch > current_pitch) {
				current_pitch++;
			} else {
				current_pitch--;
			}
		}	
	}
	
	public void send_status() {
		OSCMessage osc_message = new OSCMessage();
		osc_message.addArgument(new String("wiimote"));
		//pitch and roll
	
		
		osc_message.addArgument(new Float(current_roll));
		osc_message.addArgument(new Float(current_pitch));
	//	label.setText("pitch = " + current_pitch + "(" + target_pitch + "), roll = " + current_roll + "(" + target_roll + ")");
		label.setText("distance = " + device.getDistance());
		
		//buttons
		
		NuPlayWiiMoteButtons buttons = device.getButtons();
		osc_message.addArgument(new Boolean(buttons.button_A));
		osc_message.addArgument(new Boolean(buttons.button_B));
		osc_message.addArgument(new Boolean(buttons.button_Up));
		osc_message.addArgument(new Boolean(buttons.button_Down));
		osc_message.addArgument(new Boolean(buttons.button_Left));
		osc_message.addArgument(new Boolean(buttons.button_Right));
		osc_message.addArgument(new Boolean(buttons.button_Home));
		osc_message.addArgument(new Boolean(buttons.button_Plus));
		osc_message.addArgument(new Boolean(buttons.button_Minus));
		osc_message.addArgument(new Boolean(buttons.button_One));
		osc_message.addArgument(new Boolean(buttons.button_Two));
		//acceleration
		osc_message.addArgument(new Float(device.nGetAccelX()));
		osc_message.addArgument(new Float(device.nGetAccelY()));
		osc_message.addArgument(new Float(device.nGetAccelZ()));
		//dots
		for(int i=0; i < 4; i++) {
			float dot = device.nGetDotSize(i);
			if (dot != -1) {
				last_x[i] = device.nGetDotX(i);
				last_y[i] = device.nGetDotX(i);
			}
			osc_message.addArgument(new Float(device.nGetDotSize(i)));
			osc_message.addArgument(new Float(last_x[i]));
			osc_message.addArgument(new Float(last_y[i]));

		}
		//distance
		osc_message.addArgument(new Float(device.getDistance()));
	
		osc_message.setAddress("/nuplay/channel_" + (this.channel +1));
		packet.setData(osc_message.getByteArray());
		try {
			client_socket.send(packet);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
